package scantask.client.presentationLayer;

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.view.orbit.OrbitView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import scantask.client.domainLayer.ControllerInterface;
import scantask.containers.Benchmark;
import scantask.containers.exceptions.InvalidCoordinateValueException;
import scantask.server.domainLayer.Exceptions.ControllerException;
import scantask.server.domainLayer.Exceptions.LoginException;

public class BenchmarksPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// The world window of NASA.
	final private WorldWindow _wwd;
	
	// The client controller.
	private ControllerInterface _controller;
	
	// The combo box.
	final private JComboBox _combo;
	
	public BenchmarksPanel(WorldWindow wwd, ControllerInterface controller) {
		
		super();
		this.setLayout(null);
		this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
	
		this._wwd = wwd;
		this._controller = controller;
		
		JLabel choose = new JLabel();
		choose.setText("Choose:");
		choose.setBounds(10, 10, 100, 25);
        this.add(choose);
        
        this._combo = new JComboBox();
        this._combo.setBounds(10, 45, 160, 25);
        this.add(this._combo);
        
        JButton go = new JButton("GO");
        go.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				int selection = BenchmarksPanel.this._combo.getSelectedIndex();
				Benchmark bench;
				try {
					bench = BenchmarksPanel.this._controller.
							getBenchmarks().get(selection);
					BenchmarksPanel.this._wwd.getView().goTo(new Position(
							LatLon.fromDegrees(
									bench.getCoordinate().getY(),
									bench.getCoordinate().getX()),
									FunctionalityPanel.DEFAULT_ZOOM),
									FunctionalityPanel.DEFAULT_ZOOM);
				} catch (LoginException e) {
				}
			}
        	
        });
        go.setBounds(10, 80, 160, 25);
        this.add(go);
        
        JButton store = new JButton("Store current position");
        store.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Position pos = ((OrbitView)BenchmarksPanel.this._wwd.getView()).
										getCenterPosition();
				String name = JOptionPane.showInputDialog(null,
						"Enter a name for the new benchmark",
						"Adding new benchmark", JOptionPane.INFORMATION_MESSAGE);
				if ( (name != null) && (!name.equals("")) ){
					try {
						BenchmarksPanel.this._controller.addBenchmark(
								name,
								pos.getLongitude().getDegrees(),
								pos.getLatitude().getDegrees());
						BenchmarksPanel.this.updateComboBox();
						BenchmarksPanel.this.update();
					} catch (LoginException e) {
					} catch (InvalidCoordinateValueException e) {
					} catch (ControllerException e) {
					}
				}
			}
        	
        });
        store.setBounds(10, 115, 160, 25);
        this.add(store);
		
	}
	
	public void updateComboBox(){
		try {
			this._combo.removeAllItems();
			Vector<Benchmark> vec = this._controller.getBenchmarks();
			for (int i = 0; i<vec.size(); i++){
				this._combo.addItem(vec.get(i).getName());
			}
		} catch (LoginException e1) {
		}
	}
	
	// Update Globe.
    private void update()
    {
        this._wwd.redraw();
    }

}
